import {
	getToken
} from '@/utils/auth'

// 登录页面
const loginPage = "pages/mine/index"

// 页面白名单
const whiteList = [
	'/pages/home/index', '/pages/mine/index'
]

// 检查地址白名单
function checkWhite(url) {
	const path = url.split('?')[0]
	return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			if (!getToken()) {
				if (checkWhite(to.url)) {
					return true
				}
				uni.showToast({
					title: '请登录',
					icon: 'error',
					duration: 1000,
					success: function(res) {
						console.log(to);
						var pages = getCurrentPages();
						console.log(pages[0].route);
						// 判断是否在登录页
						if (pages[0].route == loginPage) {
							return false
						} else {
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/mine/index'
								});
							}, 1000)
							return false
						}
					}
				});
				return false
			}
		},

		fail(err) {
			console.log(err)
		}
	})
})
